package com.yswy.app.controller;

import com.auth0.jwt.interfaces.Claim;
import com.yswy.app.dto.AddUserLookPlayLogVo;
import com.yswy.app.service.AppUserService;
import com.yswy.app.service.TgCallBackIdService;
import com.yswy.app.service.TheaterPlayAppService;
import com.yswy.app.service.UserMoneyService;
import com.yswy.common.exception.LoginException;
import com.yswy.common.model.ResultModel;
import com.yswy.common.utils.TokenTool;
import com.yswy.common.vo.dto.BaseFilter;
import com.yswy.domain.entity.AppUser;
import com.yswy.theater.service.*;
import com.yswy.theater.vo.req.ListTheaterPlayGatherReqVo;
import com.yswy.theater.vo.req.ListTheaterPlayReqVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

import java.util.Map;

import static com.yswy.domain.common.util.TokenUtils.getTokenToken;
import static com.yswy.domain.common.util.TokenUtils.getUserId;

@RestController
@RequestMapping("/theater")
@Api(description = "剧场")
public class TheaterController {
    @Autowired
    private TheaterHotSearchService theaterHotSearchService;
    @Autowired
    private TheaterPlayBindTypeService theaterPlayBindTypeService;
    @Autowired
    private TheaterPlayGatherService theaterPlayGatherService;
    @Autowired
    private TheaterPlayTypeService theaterPlayTypeService;
    @Autowired
    private TheaterPlayLookLogService theaterPlayLookLogService;
    @Autowired
    private TheaterPlayUnlockService theaterPlayUnlockService;
    @Autowired
    private TheaterPlayAppService theaterPlayAppService;
    @Autowired
    private TheaterPlayService theaterPlayService;
    @Autowired
    private UserMoneyService userMoneyService;
    @Autowired
    private AppUserService appUserService;

    @Autowired
    private TgCallBackIdService tgCallBackIdService ;

    @Value("${token.sk}")
    private String tokenSk;

    @GetMapping("/getActiveTypeByUserID")
    @ApiOperation(value = "获取用户来源短剧")
    public ResultModel getActiveTypeByUserID(HttpServletRequest request) {
        return ResultModel.ok(tgCallBackIdService.getActiveTypeByUserID(getUserId(request)));
    }

    @PostMapping("/getTheaterPlayByType")
    @ApiOperation(value = "短剧列表")
    public ResultModel getTheaterPlayByType(  @RequestBody ListTheaterPlayReqVo listTheaterPlayReqVo ,
                                            HttpServletRequest request) {
        AppUser appUser = getUser(getTokenToken(request));
        return ResultModel.ok(theaterPlayAppService.getTheaterPlayByType(listTheaterPlayReqVo,appUser));
    }

    @PostMapping("/ListTheaterPlayGather")
    @ApiOperation(value = "短剧数据列表")
    public ResultModel ListTheaterPlayGather(  @RequestBody ListTheaterPlayGatherReqVo listTheaterPlayGatherReqVo ,
                                              HttpServletRequest request) {
        Long userId = getUserId(request);
        Boolean isVip = appUserService.vipBecomeDue(userId , null);
        return ResultModel.ok(theaterPlayGatherService.ListTheaterPlayGather(listTheaterPlayGatherReqVo ,userId , isVip ));
    }

    @PostMapping("/usdByTheaterPlayUnlock")
    @ApiOperation(value = "解锁")
//    @SecurityParameter()
    public ResultModel usdByTheaterPlayUnlock(  @RequestBody AddUserLookPlayLogVo addUserLookPlayLogVo,
                                               HttpServletRequest request) {
        Long userId = getUserId(request);
        return ResultModel.ok(userMoneyService.usdByTheaterPlayUnlock( addUserLookPlayLogVo,  userId ));
    }

    /** 添加短剧收藏 **/
    @GetMapping("/addTheaterPlayCollect")
    @ApiOperation(value = "添加短剧收藏")
    public ResultModel addTheaterPlayCollect(@RequestParam(value = "playId") Long playId
            , HttpServletRequest request) {
        Long userId = getUserId(request);
        theaterPlayLookLogService.addTheaterPlayLookLogCollect(playId , userId );
        return ResultModel.ok();
    }
    /** 删除短剧收藏 **/
    @GetMapping("/delTheaterPlayCollect")
    @ApiOperation(value = "删除短剧收藏")
    public ResultModel delTheaterPlayCollect(@RequestParam(value = "playId") Long playId
            , HttpServletRequest request) {
        Long userId = getUserId(request);
        theaterPlayLookLogService.delTheaterPlayLookLogCollect(playId , userId );
        return ResultModel.ok();
    }

    /** 获取收藏 **/
    @PostMapping("/listTheaterPlayCollect")
    @ApiOperation(value = "获取收藏列表")
    public ResultModel listTheaterPlayCollect(@RequestBody BaseFilter baseFilter, HttpServletRequest request) {
        Long userId = getUserId(request);
        return ResultModel.ok(theaterPlayLookLogService.listTheaterPlayLookLogCollect(userId ,baseFilter));
    }

    /** 添加短剧观看记录 **/
    @GetMapping("/addTheaterPlayLookLog")
    @ApiOperation(value = "添加短剧观看记录")
    public ResultModel addTheaterPlayLookLog(@RequestParam(value = "playId") Long playId
                                      ,@RequestParam(value = "episodes") Integer episodes
            , HttpServletRequest request) {
        Long userId = getUserId(request);
        theaterPlayLookLogService.addTheaterPlayLookLog(playId , userId , episodes);
        return ResultModel.ok();
    }

    /** 获取观看记录 **/
    @PostMapping("/listTheaterPlayLookLog")
    @ApiOperation(value = "获取观看记录")
//    @SecurityParameter()
    public ResultModel listTheaterPlayLookLog(@RequestBody BaseFilter baseFilter, HttpServletRequest request) {
        Long userId = getUserId(request);
        return ResultModel.ok(theaterPlayLookLogService.listTheaterPlayLookLog(userId ,baseFilter));
    }

    /** 点赞短剧 **/
    @GetMapping("/updateLikeNum")
    @ApiOperation(value = "点赞")
    public ResultModel updateLikeNum(@RequestParam(value = "playId") Long playId
            , HttpServletRequest request) {

        return ResultModel.ok(theaterPlayService.updateLikeNum(playId));
    }


    private AppUser getUser(String token ){
        Map<String, Claim> seedss = null ;
        try{
            seedss = TokenTool.verifyToken(token,tokenSk);
        }catch (Exception e){
            throw new LoginException("登录过期请重新登录");
        }
        Long uid = Long.parseLong(seedss.get("id").asString()) ;
        AppUser appUser = appUserService.getById(uid);
        return appUser ;
    }

}
